﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nGSO
{
    public class RandomizeSelector: INeighbourSelector
    {
        private static readonly Random _random = new Random();

        public Agent GetAgentToMoveToward(Agent forAgent)
        {
            double neighborLuceferinSum = 0;

            double probability = _random.NextDouble();

            double probabilitySum = 0;

            foreach (var neighbor in forAgent.Neighbors)
            {
                neighborLuceferinSum += neighbor.Luciferin - forAgent.Luciferin;
            }

            foreach (var neighbour in forAgent.Neighbors)
            {
                neighbour.MovingProbability = (neighbour.Luciferin - forAgent.Luciferin) / neighborLuceferinSum;
                probabilitySum += neighbour.MovingProbability;
                if (probabilitySum > probability)
                    return neighbour;
            }

            throw new ApplicationException("Agent to move forward was not selected ");
        }
    }
}
